<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button>fetch发送请求</button>
    <script>
        const api_login = 'http://ajax-api.itheima.net/api/login'
        const data = {username:'admin', password:'123456'}

        const btn = document.querySelector('button')
        btn.addEventListener('click', () => {

            fetch(api_login, {
                method:'post',
                // 1. 要注意设置headers请求头
                headers:{
                    'content-type': 'application/json'
                },
                // 2. 参数是写到body这个属性上的
                body: JSON.stringify(data)
            }).then(res => res.json()).then(res => {
                console.log(res)
            }).catch(err => {
                console.log(err)
            })
        })
    </script>
</body>
</html>